home *** CD-ROM | disk | FTP | other *** search
- /* Internet LPD Server printer permissions check
- * written by David Johnson (dave@cs.olemiss.edu)
- * portions of code extracted from PLP.
- *
- * This code is in the public domain.
- *
- * Revision History:
- *
- * Revision 1.1 91/09/27 dave (from Hans-Juergen)
- * Changed definition of D_PERMISSION to Lpdpermission
- *
- * Revision 1.0 91/09/04 dave
- * Initial Release
- *
- */
- #include <stdio.h>
- #include <fcntl.h>
- #include <ctype.h>
- #include <time.h>
- #include <sys/stat.h>
- #ifdef __TURBOC__
- #include <io.h>
- #include <dir.h>
- #endif
- #include "global.h"
- #include "mbuf.h"
- #include "proc.h"
- #include "iface.h"
- #include "socket.h"
- #include "dirutil.h"
- #include "commands.h"
- #include "files.h"
- #include "lp.h"
- #include "lpd.h"
-
- /* local functions - exported to lpd.c */
- int check_permission __ARGS((char *host, char *user, char *queue, int *priority));
-
- /* local functions - private */
- static int matches __ARGS((char *string, char *pattern));
-
- /*
- * Check specified parameters against permissions file
- *
- * Algorithm design ideas borrowed from PLP
- */
- check_permission( host, user, queue, priority )
- char *host, *user, *queue;
- int *priority;
- {
- char f_host[64], f_user[32], f_queue[32], f_priority[2];
- char buffer[256], *cp;
- int permission = 0, done = 0, i;
- int negate, tilde;
- FILE *fp;
-
- #ifdef LPD_DEBUG
- tprintf( "check_permission( %s, %s, %s, %c ) ",
- host, user, queue, *priority );
- tflush();
- #endif
-
- if( (fp = fopen( Lpdpermission, "r" )) == NULL )
- return -1;
- while( !done && fgets( buffer, sizeof( buffer ), fp ) != NULL ) {
- if( buffer[0] == '#' || buffer[0] == '\n' )
- continue;
- tilde = negate = 0;
- cp = buffer;
- if( *cp == '~' ) {
- cp++;
- tilde = 1;
- }
- if( *cp == '!' ) {
- cp++;
- negate = 1;
- }
- i = sscanf( cp, "%s%s%s%s", f_host, f_user, f_queue,
- f_priority );
- if( i == 0 )
- continue;
- if( i < 4 ) {
- /* display error message */
- return( permission );
- }
-
- if( !matches( host, f_host ) ) { /* failed */
- if( tilde )
- done = 1;
- continue;
- }
-
- if( !matches( user, f_user ) ) {
- if( tilde )
- done = 1;
- continue;
- }
-
- if( !matches( queue, f_queue ) ) {
- if( tilde )
- done = 1;
- continue;
- }
-
- /*
- * parameters matched this line. However, if a tilde is
- * present, this line is only for filtering and the
- * paramaters must also match another. Continue looking.
- */
- if( tilde )
- continue;
-
- /*
- * Now a match was really found. Check priority.
- */
- if( priority && *f_priority && *f_priority != '*' )
- if( *priority < *f_priority ) /* too high */
- *priority = *f_priority;
-
- done = permission = 1;
- if( negate )
- permission = 0;
- /*
- * Will fall out of loop (done = 1)
- */
- }
- fclose( fp );
-
- #ifdef LPD_DEBUG
- tprintf( "= %d(%c)\n", permission, *priority );
- tflush();
- #endif
- return( permission );
- }
-
- /*
- * Compare a specified string against a (possible) wildcard string
- *
- * Algorithm borrowed from PLP
- */
- int
- matches( string, pattern )
- char *string, *pattern;
- {
- int c;
-
- if( string == NULL || pattern == NULL )
- return 1;
-
- while( (c = *pattern) != NULL ) {
- switch( c ){
- /*
- * match 0 or more characters in the string
- */
- case '*':
- if( matches( string, pattern+1 ) )
- return 1;
- if( *string && matches( string+1, pattern ) )
- return 1;
- return 0;
- case '?':
- if( *string == NULL )
- return 0;
- break;
- default:
- if( c != *string )
- return 0;
- break;
- }
- ++string;
- ++pattern;
- }
- return( *pattern == *string );
- }
-